<%@ page import="org.apache.commons.io.FileUtils"%>
<%@ page import="uniLearn.control.*"%>
<%@ page import="uniLearn.data.*"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Hashtable"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.math.BigDecimal"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%-- <%@ page import="java.net.URLEncoder" %> --%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%!
	//Define Error Message
	static final String ERR_LOGIN_FAIL = "Incorrect name or password, Please login again.";

	//Define upload file size
	static final long MAX_FILE_SIZE = FileUtils.ONE_MB * 4;
	static final long MAX_REQUEST_SIZE = FileUtils.ONE_MB * 16 + FileUtils.ONE_KB;
	
	//Define Session attribute name
	static final String SESSION_LOGIN_USER = 	"loginUser";
	static final String SESSION_SELECT_UNIT = 	"selectUnit";
	
	//Define reauest parameter name
	static final String PAR_LOGIN_NAME =	"login_name";
	static final String PAR_PASSWORD =		"password";
	static final String PAR_UNIT_ID = 		"unit_id";
	static final String PAR_ASSIGNMENT_ID = "ac_id";
	static final String PAR_ASSIGNMENT_NAME = "ac_name";
	static final String PAR_SUBMIT_BY = 	"submit_by";
	static final String PAR_MARK = 			"mark_";
	static final String PAR_FEEDBACK = 		"feedback_"; 
	static final String PAR_TOTAL_MARK = 	"total_mark";
	static final String PAR_TOTAL_MARK_LB = "total_mark_lable";
	static final String PAR_UPLOAD_FILE = 	"upload_file";
	static final String PAR_AM_ORDER = 		"am_order";
	static final String PAR_USER_TYPE =		"user_type";
	static final String PAR_NAMES_LIST = 	"name_list";
	
	//Define Redirect URL
	//main page of user
	static final String URL_STUDENT = "Student/Student_index.jsp";
	static final String URL_STAFF = "Staff/Staff_index.jsp";
	static final String URL_ADMIN = "Admin/admin_index.jsp";
	//In all folders folder
	static final String URL_HOMEPAGE = "../index.html";
	static final String URL_DOWNLOAD_PROCESS = "../download_process.jsp";
	static final String URL_DOWNLOAD_ASSIGNMENT = "f_download_assignment.jsp";
	static final String URL_DOWNLOAD_MATERIAL = "f_download_material.jsp";
	static final String URL_VIEW_DETAIL_RESULT = "f_view_detail_result.jsp";
	static final String URL_VIEW_MS = "f_view_ms.jsp";
	static final String URL_SELECT_ASSIGNMENT = "f_assignment_select.jsp";
	static final String URL_SELECT_UNIT = "f_unit_select.jsp";
	static final String URL_VIEW_ASS_JS = "../js/f_view_assignment.js";
	static final String URL_JQUERY = "../js/jquery-1.6.2.js";
	//To production with jQuery, use the production version of jQuary
	//static final String URL_JQUERY = "../js/jquery-1.6.2.min.js";
	
	//In staff & admin folder
	static final String URL_MODIFY_ASSIGNMENT = "f_modify_assignment_collector.jsp";
	static final String URL_MODIFY_MS = "f_modify_ms.jsp";
	static final String URL_MODIFY_AM = "f_modify_material.jsp";
	static final String URL_CONFIRM_ASSIGNMENT = "f_confirm_assignment.jsp";
	static final String URL_CONFIRM_AM = "f_confirm_material.jsp";
	//static final String URL_CONFIRM_MS = "f_confirm_ms.jsp"; //Not in use @ jsp, but on javascript: f_modify_ms.js
	
	//In student folder
	static final String URL_UPLOAD_ASSIGNMENT = "f_upload_submission.jsp";
	static final String URL_CONFIRM_SUBMISSION = "f_confirm_submission.jsp";
	//In staff folder
	static final String URL_VIEW_ASSIGNMENT = "f_staff_view_assignment.jsp";
	static final String URL_VIEW_SUBMITED = "f_staff_view_submit_assignment.jsp";
	static final String URL_MARK_SUBMIT = "f_staff_mark_submit_assignment.jsp";
	static final String URL_CONFIRM_MARK = "f_staff_confirm_marking.jsp";

	//In admin folder
	static final String URL_ADMIN_VIEW_SA = "f_admin_view_submit_assignment.jsp";
	static final String URL_MODIFY_USER_UNIT = "f_modify_users_unit.jsp";
	static final String URL_VIEW_USER_UNIT = "f_view_user_unit.jsp";
	
	//image element
	static final String IMG_YES =		"<img src='../images/button/yes.png' class='img_icon' alt='Yes'/>";
	static final String IMG_NO =		"<img src='../images/button/no.png' class='img_icon' alt='No'/>";
	static final String IMG_VIEW =		"<img src='../images/button/View.gif' class='img_btn' alt='View'/>";
	static final String IMG_MODIFY =	"<img src='../images/button/modify.gif' class='img_btn' alt='Modify'/>";
	static final String IMG_SUBMIT =	"<img src='../images/button/submit.gif' class='img_btn' alt='Submit'/>";
	static final String IMG_RESET =		"<img src='../images/button/reset.gif' class='img_btn' alt='Reset'/>";
	static final String IMG_DOWNLOAD =	"<img src='../images/button/download.png' class='img_icon' alt='Download Now!'/>";
	static final String IMG_LOGOUT =	"<img src='../images/button/logout.gif' class='img_btn' alt='Logout Now!'/>";
	
	//shared function
	static boolean loginedChecking(HttpSession session, UsersType reqType) {
		Object loginUser = session.getAttribute(SESSION_LOGIN_USER);
		if(!objectChecking(loginUser, Users.class))
			return false;
		
		return ((Users)loginUser).getUserType() == reqType;
	}
	
	static boolean selectUnitCheck(HttpSession session) {
		return objectChecking(session.getAttribute(SESSION_SELECT_UNIT), Units.class);
	}
	
	static boolean objectChecking(Object sessionValue, Class targetClass) {
		if(sessionValue == null)
			return false;
		
		return targetClass.isInstance(sessionValue);
	}
	
	static boolean isEmptyString(String str) {
		return (str == null || str.isEmpty());
	}
	
	static String addParmeterToUrl(String url, String name, String value) throws Exception {
		String newURL = url + "?"+ name+ "="+ value;
		//return URLEncoder.encode(newURL,"UTF-8");
		return newURL;
	}
	
	static String addParmeterToUrl(String url, Hashtable<String, String> parTable) throws Exception {
		StringBuilder newURL = new StringBuilder(url);
		if(!parTable.isEmpty()) {
			newURL.append("?");
			java.util.Enumeration<String> keys = parTable.keys();
			String key = keys.nextElement();
			//add the first key & values to url
			newURL.append(key);
			newURL.append("=");
			newURL.append(parTable.get(key));
			//add the others
			while(keys.hasMoreElements()) {
				key = keys.nextElement();
				newURL.append("&");
				newURL.append(key);
				newURL.append("=");
				newURL.append(parTable.get(key));	 
			}
		}
		
		//return URLEncoder.encode(newURL.toString(),"UTF-8");
		return newURL.toString();
	}
	
	static String formatDateTime(Date datetime) {
		return (datetime != null) ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datetime) : "";
	}
	
	static String muiliteLineString(String str) {
		return str.replaceAll("\r\n", "<br />");
	}
	
	static String genSelectOption(int start, int end, int target) {
		StringBuilder result = new StringBuilder(21 * (end - start + 1));
		for(int i = start; i <= end; i++) {
			if(i == target)
				result.append("<option selected=\"selected\">");
			else
				result.append("<option>");
			result.append(i);
			result.append("</option>\r\n");
		}
		return result.toString();
	}
%>